<title>配送方式表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="Api/CoreCmsShip/GetIndex" lay-done="layui.data.done(d);">
</script>
<div class="table-body">
    <table id="LAY-app-CoreCmsShip-tableBox" lay-filter="LAY-app-CoreCmsShip-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-CoreCmsShip-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
    </div>
</script>
<script type="text/html" id="LAY-app-CoreCmsShip-tableBox-bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    var indexData;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coreHelper'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , view = layui.view;

                //重载form
                form.render();
                var searchwhere;
                //监听搜索
                form.on('submit(LAY-app-CoreCmsShip-search)',
                    function (data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reload('LAY-app-CoreCmsShip-tableBox', { where: field });
                    });
                //数据绑定
                table.render({
                    elem: '#LAY-app-CoreCmsShip-tableBox',
                    url: 'Api/CoreCmsShip/GetPageList',
                    method: 'POST',
                    toolbar: '#LAY-app-CoreCmsShip-toolbar',
                    defaultToolbar: ['filter', 'print', 'exports'],
                    height: 'full-127',//无面包屑127,搜索框189,1行62
                    page: true,
                    limit: 30,
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    text: { none: '暂无相关数据' },
                    cols: [
                        [
                            { field: 'id', title: '序列', width: 80, sort: false },
                            { field: 'name', title: '配送方式名称', sort: false },
                            { field: 'logiName', title: '物流公司名称', sort: false },
                            { field: 'logiCode', title: '物流公司编码', sort: false },
                            //{ field: 'isCashOnDelivery', title: '是否货到付款', width: 120, templet: '#switch_isCashOnDelivery', sort: false, unresize: true },
                            { field: 'isDefault', title: '是否默认', width: 95, templet: '#switch_isDefault', sort: false, unresize: true },
                            { field: 'isfreePostage', title: '是否包邮', width: 95, templet: '#switch_isfreePostage', sort: false, unresize: true },
                            { field: 'sort', title: '排序', sort: false, width: 105 },
                            {
                                field: 'status', title: '状态', sort: false, width: 105, templet: function (d) {
                                    if (d.status === 1) {
                                        return "<button type=\"button\" class=\"layui-btn layui-btn-xs\">启用</button>";
                                    } else if (d.status === 2) {
                                        return "<button type=\"button\" class=\"layui-btn layui-btn-xs layui-btn-danger\">停止</button>";
                                    }
                                }
                            },
                            { width: 122, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsShip-tableBox-bar' }
                        ]
                    ]
                });
                //监听排序事件
                table.on('sort(LAY-app-CoreCmsShip-tableBox)', function (obj) {
                    table.reload('LAY-app-CoreCmsShip-tableBox', {
                        initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                        where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                            orderField: obj.field, //排序字段
                            orderDirection: obj.type //排序方式
                        }
                    });
                });
                //头工具栏事件
                table.on('toolbar(LAY-app-CoreCmsShip-tableBox)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'addData':
                            doCreate();
                            break;
                    };
                });
                //监听工具条
                table.on('tool(LAY-app-CoreCmsShip-tableBox)',
                    function (obj) {
                        if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        }
                    });
                //执行创建操作
                function doCreate() {
                    coreHelper.Post("Api/CoreCmsShip/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '创建数据',
                                area: ['90%', '90%'],
                                id: 'LAY-popup-CoreCmsShip-create',
                                success: function (layero, index) {
                                    view(this.id).render('shop/ship/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsShip-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.isCashOnDelivery = field.isCashOnDelivery == 'on';
                                                field.isdefaultAreaFee = field.isdefaultAreaFee == 'on';
                                                field.isDefault = field.isDefault == 'on';
                                                field.isfreePostage = field.isfreePostage == 'on';

                                                if (field.areaType == "2") {
                                                    var keys = Object.keys(field);
                                                    var keysCount = 0;
                                                    for (var i = 0; i < keys.length; i++) {
                                                        if (keys[i].indexOf('areaId') != -1) {
                                                            keysCount++;
                                                        }
                                                    }

                                                    if (keysCount <= 0) {
                                                        layer.msg("选择【部分地区】请至少设置一个地区明细。");
                                                        return false;
                                                    }

                                                    field.keysCount = keysCount;
                                                    var areaFee = [];
                                                    for (var i = 0; i < keysCount; i++) {
                                                        let areaModel = {};
                                                        var areaId = 'areaId[' + i + ']';
                                                        var continueunitAreaPrice = 'continueunitAreaPrice[' + i + ']';
                                                        var firstunitAreaPrice = 'firstunitAreaPrice[' + i + ']';
                                                        var areaParentName = 'areaParentName[' + i + ']';
                                                        areaModel.areaValue = field[areaId];

                                                        areaModel.area = '';
                                                        if (areaModel.areaValue) {
                                                            var arr = [];
                                                            var obj = JSON.parse(areaModel.areaValue);
                                                            if (obj.length > 0) {
                                                                for (var j = 0; j < obj.length; j++) {
                                                                    arr.push(obj[j].id);
                                                                }
                                                                areaModel.area = arr.join(',');
                                                            }
                                                        }
                                                        areaModel.areaParentName = field[areaParentName];
                                                        areaModel.continueunitAreaPrice = field[continueunitAreaPrice];
                                                        areaModel.firstunitAreaPrice = field[firstunitAreaPrice];
                                                        areaFee.push(areaModel);
                                                    }
                                                    field.areaFee = JSON.stringify(areaFee);
                                                } else {
                                                    field.areaFee = "";
                                                }

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsShip/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsShip-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/CoreCmsShip/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '编辑数据',
                                area: ['90%', '90%'],
                                id: 'LAY-popup-CoreCmsShip-edit',
                                success: function (layero, index) {
                                    view(this.id).render('shop/ship/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsShip-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.isCashOnDelivery = field.isCashOnDelivery == 'on';
                                                field.isdefaultAreaFee = field.isdefaultAreaFee == 'on';
                                                field.isDefault = field.isDefault == 'on';
                                                field.isfreePostage = field.isfreePostage == 'on';
                                                console.log('field.areaType：' + field.areaType);
                                                if (field.areaType == "2") {
                                                    var keys = Object.keys(field);
                                                    var keysCount = 0;
                                                    for (var i = 0; i < keys.length; i++) {
                                                        if (keys[i].indexOf('areaId') != -1) {
                                                            keysCount++;
                                                        }
                                                    }

                                                    if (keysCount <= 0) {
                                                        layer.msg("选择【部分地区】请至少设置一个地区明细。");
                                                        return false;
                                                    }

                                                    var areaFee = [];
                                                    for (var i = 0; i < keysCount; i++) {
                                                        let areaModel = {};
                                                        var areaId = 'areaId[' + i + ']';
                                                        var continueunitAreaPrice = 'continueunitAreaPrice[' + i + ']';
                                                        var firstunitAreaPrice = 'firstunitAreaPrice[' + i + ']';
                                                        var areaParentName = 'areaParentName[' + i + ']';
                                                        areaModel.areaValue = field[areaId];

                                                        areaModel.area = '';
                                                        if (areaModel.areaValue) {
                                                            var arr = [];
                                                            var obj = JSON.parse(areaModel.areaValue);
                                                            if (obj.length > 0) {
                                                                for (var j = 0; j < obj.length; j++) {
                                                                    arr.push(obj[j].id);
                                                                }
                                                                areaModel.area = arr.join(',');
                                                            }
                                                        }
                                                        areaModel.areaParentName = field[areaParentName];
                                                        areaModel.continueunitAreaPrice = field[continueunitAreaPrice];
                                                        areaModel.firstunitAreaPrice = field[firstunitAreaPrice];
                                                        areaFee.push(areaModel);
                                                    }
                                                    field.areaFee = JSON.stringify(areaFee);
                                                } else {
                                                    field.areaFee = "";
                                                }


                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsShip/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsShip-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行单个删除
                function doDelete(obj) {
                    layer.confirm('确定删除吗？删除后将无法恢复。', function (index) {
                        coreHelper.Post("Api/CoreCmsShip/DoDelete", { id: obj.data.id }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            table.reload('LAY-app-CoreCmsShip-tableBox');
                            layer.msg(e.msg);
                        });
                    });
                }

                layui.form.on('switch(switch_isCashOnDelivery)', function (obj) {
                    coreHelper.Post("Api/CoreCmsShip/DoSetisCashOnDelivery", { id: this.value, data: obj.elem.checked }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        table.reload('LAY-app-CoreCmsShip-tableBox');
                        layer.msg(e.msg);
                    });
                });

                layui.form.on('switch(switch_isdefaultAreaFee)', function (obj) {
                    coreHelper.Post("Api/CoreCmsShip/DoSetisdefaultAreaFee", { id: this.value, data: obj.elem.checked }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        table.reload('LAY-app-CoreCmsShip-tableBox');
                        layer.msg(e.msg);
                    });
                });

                layui.form.on('switch(switch_isDefault)', function (obj) {
                    coreHelper.Post("Api/CoreCmsShip/DoSetisDefault", { id: this.value, data: obj.elem.checked }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        table.reload('LAY-app-CoreCmsShip-tableBox');
                        layer.msg(e.msg);
                    });
                });

                layui.form.on('switch(switch_isfreePostage)', function (obj) {
                    coreHelper.Post("Api/CoreCmsShip/DoSetisfreePostage", { id: this.value, data: obj.elem.checked }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        table.reload('LAY-app-CoreCmsShip-tableBox');
                        layer.msg(e.msg);
                    });
                });

            });
    };
</script>

<!--设置是否货到付款-->
<script type="text/html" id="switch_isCashOnDelivery">
    <input type="checkbox" name="switch_isCashOnDelivery" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="switch_isCashOnDelivery" {{ d.isCashOnDelivery ? 'checked' : '' }}>
</script>

<!--设置是否按地区设置配送费用-->
<script type="text/html" id="switch_isdefaultAreaFee">
    <input type="checkbox" name="switch_isdefaultAreaFee" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="switch_isdefaultAreaFee" {{ d.isdefaultAreaFee ? 'checked' : '' }}>
</script>

<!--设置是否默认-->
<script type="text/html" id="switch_isDefault">
    <input type="checkbox" name="switch_isDefault" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="switch_isDefault" {{ d.isDefault ? 'checked' : '' }}>
</script>

<!--设置是否包邮-->
<script type="text/html" id="switch_isfreePostage">
    <input type="checkbox" name="switch_isfreePostage" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="switch_isfreePostage" {{ d.isfreePostage ? 'checked' : '' }}>
</script>